package com.dingreading.cloud.soms.service.impl;

import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.soms.entity.ReceiptRegisterImg;
import com.dingreading.cloud.soms.entity.table.ReceiptRegisterImgTableDef;
import com.dingreading.cloud.soms.mapper.ReceiptRegisterImgMapper;
import com.dingreading.cloud.soms.service.ReceiptRegisterImgService;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2025-05-26
 */
@Service
public class ReceiptRegisterImgServiceImpl extends BaseServiceImpl<ReceiptRegisterImgMapper, ReceiptRegisterImg, ReceiptRegisterImgTableDef> implements ReceiptRegisterImgService {

    @Override
    protected ReceiptRegisterImgTableDef getTable() {
        return ReceiptRegisterImgTableDef.receiptRegisterImg;
    }

    @Override
    public boolean unbindReceipt(String storeUid, String orderCode, String receiptUid, String staffUid, String staffName) {
        QueryCondition condition = table.receiptUid.eq(receiptUid)
                .and(table.storeUid.eq(storeUid))
                .and(table.orderCode.eq(orderCode));
        return UpdateChain.of(ReceiptRegisterImg.class)
                .set(table.orderCode, "")
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(condition)
                .update();
    }

    @Override
    public List<String> urlListByReceiptUidAndIdsNotIn(String storeUid, String receiptUid, List<Long> ids) {
        QueryCondition condition = table.receiptUid.eq(receiptUid)
                .and(table.storeUid.eq(storeUid))
                .and(table.id.notIn(ids));
        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.url)
                .from(table)
                .where(condition);
        return getMapper().selectObjectListByQueryAs(wrapper, String.class);
    }

    @Override
    public int deleteByReceiptUidNotIn(String storeUid, String receiptUid, List<Long> ids) {
        QueryCondition condition = table.receiptUid.eq(receiptUid)
                .and(table.storeUid.eq(storeUid))
                .and(table.id.notIn(ids));
        return getMapper().deleteByCondition(condition);
    }

    @Override
    public List<String> urlListByReceiptUid(String storeUid, String receiptUid) {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.url)
                .from(table)
                .where(table.receiptUid.eq(receiptUid).and(table.storeUid.eq(storeUid)));
        return getMapper().selectObjectListByQueryAs(wrapper, String.class);
    }

    @Override
    public int deleteByReceiptUid(String storeUid, String receiptUid) {
        return getMapper().deleteByCondition(table.receiptUid.eq(receiptUid).and(table.storeUid.eq(storeUid)));
    }

    @Override
    public boolean updateInfo(String storeUid, String orderCode, String receiptUid, String staffUid, String staffName) {
        return UpdateChain.of(ReceiptRegisterImg.class)
                .set(table.storeUid, storeUid)
                .set(table.orderCode, orderCode)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(table.receiptUid.eq(receiptUid))
                .update();
    }
}
